/***********************************************************************************

    Filename:    simple_hid_dongle.c

    Description: Main program of HID dongle.

***********************************************************************************/

/***********************************************************************************
* INCLUDES
*/
#include "hal_defs.h"
#include "../common/mrfi_link.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_mcu.h"
#include "usb_hid.h"
#include "simple_hid.h"


/***********************************************************************************
* LOCAL VARIABLES
*/
static uint8 pRfData[MAX_DATA_SIZE];



/***********************************************************************************
* @fn          main
*
* @brief       This is the main entry of the RF HID application. It sets
*              distinct short addresses for the nodes, initalises and runs
*              receiver and sender tasks sequentially in an endless loop.
*
* @return      none
*/
int main(void) {
  // Initialise board peripherals
  halBoardInit();

  // Initialise USB
  usbHidInit();

  // Initialize MRFI
  mrfiLinkInit(DONGLE_ADDRESS, EB_ADDRESS, MRFI_CHANNEL);

  // Indicate that the device is initialised
  halLedSet(1);

  //  Main processing loop
  while (TRUE) {

    // Process USB standard requests
    // usbHidProcessEvents();

    // Test Code added =============
    halLedToggle(1);
    // wait
    halMcuWaitMs(1000);
    // ===============================

    // Process incoming radio traffic from HID devices
    if (mrfiLinkDataRdy()) {

      uint8 numBytes;

      // Receive RF packet
      numBytes = mrfiLinkRecv(pRfData);

      // If reception successful, ACK it and send packet to host over USB
      if (numBytes > 0) {

        if (pRfData[0] == KEYBOARD_DATA_ID && numBytes == KEYBOARD_DATA_SIZE) {
          // Process keyboard data
          usbHidProcessKeyboard(pRfData);
          halLedToggle(1);
        }

        if (pRfData[0] == MOUSE_DATA_ID && numBytes == MOUSE_DATA_SIZE) {
          // Process mouse data
          usbHidProcessMouse(pRfData);
          halLedToggle(1);
        }
      }
    }
  }

  return (0);
}
